(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.user.createRejectController', createRejectController);
	createRejectController.$inject = [ '$scope', 'Constants', 'modalService', 'gridService', 'layoutConfig', 'validatorConfig', 'authService',
			'userService' ];
	function createRejectController($scope, Constants, modalService, gridService, layoutConfig, validatorConfig, authService, userService) {
		var ctrl = this, params = $scope.$stateParams.params, errorMessage = {};
		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		ctrl.user = {};
		
		ctrl.init = function() {
			ctrl.accessRolePermission = {permission : 'accessRole', authorization : authService};
			// init role
			userService.initRole($scope, ctrl);
			// init group
			userService.initGroup($scope, ctrl);
			// init token
			userService.initToken($scope, ctrl);
			// init user dynaparm
			userService.initDynaParm($scope, ctrl);
			
			userService.loadOptions().then(function(userOptions) {
				userService.initOptions(ctrl, userOptions);
				authService.createReject(params).then(function(response) {
					if (response.data && response.data.model && response.data.task) {
						ctrl.user = response.data.model;
						ctrl.user.userRoles = ctrl.user.userRoles || [];
						ctrl.user.userGroups = ctrl.user.userGroups || [];
						ctrl.user.userTokens = ctrl.user.userTokens || [];
						ctrl.user.taskId = response.data.task.taskId;
						ctrl.resetDynaProperties();
					}
				});
			});
			
		};
		
		ctrl.checkTokenId = function(row) {
			if (ctrl.user.userTokens.length > 1) {
				for (var i = 1; i < ctrl.user.userTokens.length; i++) {
					for (var j = 0; j < ctrl.user.userTokens.length; j++) {
						if (ctrl.user.userTokens[j].tokenId != null
								&& ctrl.user.userTokens[j].tokenId != ""
								&& ctrl.user.userTokens[i].tokenId != null
								&& ctrl.user.userTokens[i].tokenId != ""
								&& ctrl.user.userTokens[j].tokenType != null
								&& ctrl.user.userTokens[j].tokenType != ""
								&& ctrl.user.userTokens[i].tokenType != null
								&& ctrl.user.userTokens[i].tokenType != "") {
							if (ctrl.user.userTokens[i].tokenId == ctrl.user.userTokens[j].tokenId
									&& ctrl.user.userTokens[i].tokenType == ctrl.user.userTokens[j].tokenType
									&& i != j) {
								modalService
										.openErrorDialogs({
											messages : [ 'js.info.user.tokenid-duplicated' ]
										});
								if (ctrl.user.userTokens[i].$$hashKey == row.$$hashKey) {
									ctrl.user.userTokens[i].tokenId = null;
									ctrl.user.userTokens[i].tokenType = null;
								} else {
									ctrl.user.userTokens[j].tokenId = null;
									ctrl.user.userTokens[j].tokenType = null;
								}
							}
						}
					}
				}
			}
		};

		ctrl.createRejectSave = function() {
			return authService.createRejectSave(ctrl.user).then(saveSuccessful, saveFailed);
		};

		ctrl.createRejectSubmit = function() {
			return authService.createRejectSubmit(ctrl.user).then(submitSuccessful, submitFailed);
		};
		
		ctrl.createRejectCancel = function() {
			modalService.openConfirmDialogs({
				'messages' : [ {msg : 'title.dialog.confirm.cancel', args : [ctrl.user.primaryKey]} ],
				'btnCallback' : {
					'ok' : function(response) {
						authService.createRejectCancel(ctrl.user).then(cancelSuccessful, cancelFailed);
					}
				}
			});
		};
		
		function submitSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.submit' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.workflow.wfSetup.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.user.list',
					'state' : 'cda.user.list',
					'params' : {}
				}]
			});
		}

		function submitFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}

		function saveSuccessful(response) {
			modalService.openInfoDialogs({
				'messages' : [ 'title.dialog.successful.save' ]
			});
			if (response.data && response.data.model) {
				angular.extend(ctrl.user, response.data.model);
			}else if (response.data.task && response.data.task.taskId) {
				authService.saveSuccessfulProcess(response.data.task).then(function(response) {
					var routerInfo = response.data || {}, state = routerInfo.processState;
					delete routerInfo.processState;
					$scope.go(state, routerInfo);
				}, angular.noop);
			}
		}

		function saveFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}
		
		function cancelSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.cancel' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.workflow.wfSetup.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.user.list',
					'state' : 'cda.user.list',
					'params' : {}
				}]
			});
		}

		function cancelFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}
		
	}
})(angular);
